package view;


import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import dao.UserDao;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class login extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;

	private static JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public login() {

		setTitle("\u9152\u5E97\u623F\u95F4\u7BA1\u7406\u7CFB\u7EDF");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 297, 273);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		
		textField = new JTextField();
		textField.setBounds(124, 47, 66, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel label_1 = new JLabel("\u5BC6  \u7801 \uFF1A");
		label_1.setBounds(54, 87, 54, 15);
		contentPane.add(label_1);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(124, 78, 66, 21);
		contentPane.add(passwordField);
		
		final JComboBox<String> cb = new JComboBox<String>();
		cb.setModel(new DefaultComboBoxModel<String>(new String[] {"\u666E\u901A\u7528\u6237", "\u7BA1\u7406\u5458"}));
		cb.setBounds(124, 115, 87, 21);
		contentPane.add(cb);
		
		JLabel label_2 = new JLabel("\u7528\u6237\u7C7B\u578B \uFF1A");
		label_2.setBounds(51, 118, 74, 15);
		contentPane.add(label_2);
		
		JButton button = new JButton("\u767B\u5F55");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String uname=textField.getText().toString();
				String pwd= passwordField.getText().toString();
				if (uname.equals("")) {
					 JOptionPane.showMessageDialog(new JFrame().getContentPane(),"用户名为空", "酒店管理系统", JOptionPane.WARNING_MESSAGE);
				}
				else{
					if(pwd.equals("")){
						 JOptionPane.showMessageDialog(new JFrame().getContentPane(),"密码为空！请输入密码！", "酒店管理系统", JOptionPane.WARNING_MESSAGE);
					}
				}
				//1为管理员0为普通用户
				int uType=0;
				if (cb.getSelectedItem().equals("管理员")) {
					 uType=1;
				}
				UserDao dao = new UserDao();
				if (dao.find(uname, pwd, uType)==null) {
					JOptionPane.showMessageDialog(new JFrame().getContentPane(), "用户名，密码或用户类型错误","酒店管理系统",JOptionPane.WARNING_MESSAGE);
					
				}else{
					frame.setVisible(false);
					operatior op=new operatior();
					op.setVisible(true);
					
					
				}
				
			}
		});
		button.setBounds(42, 176, 66, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("\u91CD\u7F6E");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField.setText("");
				passwordField.setText("");
				cb.setSelectedItem("\u666E\u901A\u7528\u6237");
			}
		});
		button_1.setBounds(124, 176, 66, 23);
		contentPane.add(button_1);
		
		JButton button_2 = new JButton("\u9000\u51FA");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setVisible(false);
			}
		});
		button_2.setBounds(205, 176, 66, 23);
		contentPane.add(button_2);
		
		JLabel label = new JLabel("用户名：");
		label.setBounds(51, 50, 54, 15);
		contentPane.add(label);
	}
	
}
